package com.yanqu.road.dao.impl.servercenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.poster.ActivityPosterInfo;
import com.yanqu.road.entity.enums.eLightType;
import com.yanqu.road.entity.language.LanguageType;
import com.yanqu.road.entity.mall.HideMallData;
import com.yanqu.road.entity.mall.ShieldMallMarkIdData;
import com.yanqu.road.entity.servercenter.*;
import com.yanqu.road.entity.servercenter.tabnotice.ChannelTabNotice;
import com.yanqu.road.entity.servercenter.tabnotice.TabNotice;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class ServerInfoDaoImpl extends TempDao {

    public static String getDataUploadInfoSql = "select * from t_s_data_upload_info where channelId = ?";
    public DataUploadInfo getDataUploadInfo(int channelId) throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDataUploadInfoSql);
            call.setInt(1, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                DataUploadInfo dataUploadInfo = new DataUploadInfo();
                dataUploadInfo.setChannelId(rs.getInt("channelId"));
                dataUploadInfo.setAppId(rs.getString("appId"));
                dataUploadInfo.setCommonBackendUrl(rs.getString("commonBackendUrl"));
                dataUploadInfo.setAnalysisBackendUrl(rs.getString("analysisBackendUrl"));
                dataUploadInfo.setAppSecret(rs.getString("appSecret"));
                return dataUploadInfo;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }



    public int getCloseServerCountdownMinutes() {
        List<ServerInfo> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_close_server_time limit 1";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());

            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                int minutes = rs.getInt("minutes");
                return minutes;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return 3;
    }

    public List<ServerInfo> getServerInfoList() {
        List<ServerInfo> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_server_list order by serverId desc";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());

            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ServerInfo serverInfo = getServerInfo(rs);
                dataList.add(serverInfo);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return dataList;
    }

    public Map<Integer, List<Integer>> getActivityServerShowMap() {
        Map<Integer, List<Integer>> map = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_cn_activity_server_show";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());

            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                int src = rs.getInt("srcChannelId");
                int dest = rs.getInt("destChannelId");
                if (!map.containsKey(src)) {
                    map.put(src, new ArrayList<>());
                }

                map.get(src).add(dest);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return map;
    }

    /**
     * 获取国服活动服用户白名单
     * @return
     */
    public HashMap<Long, HashSet<String>> getCnActivityUserList() {
        HashMap<Long, HashSet<String>> map = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_cn_activity_user";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());

            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                long serverId = Long.valueOf(rs.getInt("serverId"));
                if (!map.containsKey(serverId)) {
                    map.put(serverId, new HashSet<>());
                }
                map.get(serverId).add(rs.getString("platformUserId"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return map;
    }

    public Map<String, Long> getForbiddenIpMap() {
        Map<String, Long> res = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_forbidden_ip";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());

            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                res.put(rs.getString("forbiddenIp"), rs.getLong("forbiddenEndTime"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return res;
    }

    public boolean addForbiddenIp(String ip, long forbiddenEndTime) {
        PreparedStatement call = null;
        String sql = "insert into t_s_forbidden_ip (forbiddenIp, forbiddenEndTime) values (?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, ip);
            call.setLong(i++, forbiddenEndTime);
            if (call.executeUpdate() > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("{}:", Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    public boolean updateForbiddenIp(String ip, long forbiddenEndTime) {
        PreparedStatement call = null;
        String sql = "update t_s_forbidden_ip set forbiddenEndTime=? where forbiddenIp=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, forbiddenEndTime);
            call.setString(i++, ip);
            if (call.executeUpdate() > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("{}:", Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    public ServerInfo getServerInfoByServerId(long serverId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_server_list where serverId=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());

            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, serverId);
            rs = call.executeQuery();
            while (rs.next()) {
                ServerInfo serverInfo = getServerInfo(rs);
                return serverInfo;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public List<ServerInfo> getServerInfoListByServerIdList(List<Long> serverIdList) {
        String sql = "select * from t_s_server_list where serverId in(" + StringUtils.listToString(serverIdList, ",") + ")";
        List<ServerInfo> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());

            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ServerInfo serverInfo = getServerInfo(rs);
                dataList.add(serverInfo);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return dataList;
    }

    public Map<Long, Map<String, ServerNameLanguage>> getServerNameLanguageMap() {
        Map<Long, Map<String, ServerNameLanguage>> nameMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_server_name";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());

            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ServerNameLanguage serverNameLanguage = new ServerNameLanguage();
                serverNameLanguage.setServerId(rs.getLong("serverId"));
                serverNameLanguage.setLanguageType(rs.getString("languageType"));
                serverNameLanguage.setServerName(rs.getString("serverName"));
                if (!nameMap.containsKey(serverNameLanguage.getServerId())) {
                    nameMap.put(serverNameLanguage.getServerId(), new ConcurrentHashMap<>());
                }
                nameMap.get(serverNameLanguage.getServerId()).put(serverNameLanguage.getLanguageType(), serverNameLanguage);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return nameMap;
    }

    public Map<Integer, Long> getHideTimeMap() {
        Map<Integer, Long> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_hide_time";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());

            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                map.put(rs.getInt("channelId"), rs.getLong("hideTime"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return map;
    }

    public Map<Integer,Map<String, ChannelNotice>> getChannelNoticeMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_notice order by id desc";
        Map<Integer,Map<String, ChannelNotice>> dataMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelNotice data = new ChannelNotice();
                data.setChannelId(rs.getInt("channelId"));
                data.setLanguage(rs.getString("languageType"));
                data.setContent(rs.getString("content"));
                data.setTitle(rs.getString("title"));
                data.setId(rs.getInt("id"));
                if(StringUtils.isNullOrEmpty(data.getLanguage())){
                    data.setLanguage(LanguageType.LANGUAGE_TW);
                }
                if (!dataMap.containsKey(data.getChannelId())) {
                    dataMap.put(data.getChannelId(), new HashMap<>());
                }

                if (dataMap.get(data.getChannelId()).size() > 0) {// 已有公告
                    for (String languageType : dataMap.get(data.getChannelId()).keySet()) {
                        if (data.getId() == dataMap.get(data.getChannelId()).get(languageType).getId()) {
                            dataMap.get(data.getChannelId()).put(data.getLanguage(), data);
                        }
                        break;
                    }
                } else {// 还没有写入公告
                    dataMap.get(data.getChannelId()).put(data.getLanguage(), data);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, List<ChannelTabNotice>> getChannelTabNoticeMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_tab_notice";
        Map<Integer, List<ChannelTabNotice>> dataMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelTabNotice data = new ChannelTabNotice();
                data.setChannelId(rs.getInt("channelId"));
                data.setGameType(rs.getInt("gameType"));
                data.setLanguage(rs.getString("language"));
                data.setAppId(rs.getString("appId"));
                data.setTabNotice(JSON.parseObject(rs.getString("tabNotices"), new TypeReference<List<TabNotice>>() {}));
                if(StringUtils.isNullOrEmpty(data.getLanguage())){
                    data.setLanguage(LanguageType.LANGUAGE_TW);
                }
                int channelId = data.getChannelId();
                if (!dataMap.containsKey(channelId)) {
                    dataMap.put(channelId, new ArrayList<>());
                }
                dataMap.get(channelId).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private ServerInfo getServerInfo(ResultSet rs) throws SQLException {
        ServerInfo data = new ServerInfo();
        data.setChannelId(rs.getInt("channelId"));
        data.setOpen(rs.getBoolean("isOpen"));
        data.setServerId(rs.getLong("serverId"));
        data.setServerName(rs.getString("serverName"));
        data.setGameServerUrl(rs.getString("gameServerUrl"));
        data.setWebServerUrl(rs.getString("webServerUrl"));
        data.setOpenTime(rs.getLong("openTime"));
        data.setState(rs.getInt("state"));
        data.setWsIndex(rs.getString("wsIndex"));
        data.setMaxOnlineCount(rs.getInt("maxOnlineCount"));
        data.setOpenLoginQueue(rs.getBoolean("openLoginQueue"));
        data.setMarkStr(rs.getString("markStr"));
        String clientVerCode = rs.getString("clientVerCode");
        if(!StringUtils.isNullOrEmpty(clientVerCode)) {
            data.setClientVerCode(clientVerCode);
        }
        data.setAccessPhonePlatform(rs.getInt("accessPhonePlatform"));
        data.setServerType(rs.getInt("serverType"));
        data.setOpenSplitFlow(rs.getBoolean("openSplitFlow"));
        data.setRegisterLimit(rs.getInt("registerLimit"));
        data.setOpenZsDay(rs.getInt("openZsDay"));
        data.setCreateRoleLimit(rs.getBoolean("createRoleLimit"));
        data.setCreatePreRoleOpen(rs.getBoolean("createPreRoleOpen"));
        data.setCreatePreRoleNum(rs.getInt("createPreRoleNum"));
        data.setPreRoleNum(rs.getInt("preRoleNum"));
        data.setSpecialServer(rs.getInt("specialServer"));
        data.setCreateRoleCountLimit(rs.getInt("createRoleCountLimit"));
        data.setSpecialServerParam(rs.getInt("specialServerParam"));
        data.setNoneOption();
        return data;
    }

    public static String getChannelListSql = "select channelId,openSplitFlow,openDataAnalytics,fbFollowUrl,openTime,translateUrl,collectServerUrl,getActivityUrl,loadingBgSwitch,clientSwitch,kafkaCollect,kafkaServers from t_s_channel_list";
    public Map<Integer, ChannelInfo> getChannelListInfo() throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ChannelInfo> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getChannelListSql);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelInfo channelInfo = new ChannelInfo();
                channelInfo.setChannelId(rs.getInt("channelId"));
                channelInfo.setOpenSplitFlow(rs.getBoolean("openSplitFlow"));
                channelInfo.setOpenDataAnalytics(rs.getBoolean("openDataAnalytics"));
                channelInfo.setFbFollowUrl(rs.getString("fbFollowUrl"));
                channelInfo.setTranslateUrl(rs.getString("translateUrl"));
                channelInfo.setOpenTime(rs.getLong("openTime"));
                channelInfo.setCollectServerUrl(rs.getString("collectServerUrl"));
                channelInfo.setKafkaCollect(rs.getInt("kafkaCollect"));
                channelInfo.setKafkaServers(rs.getString("kafkaServers"));
                channelInfo.setGetActivityUrl(rs.getString("getActivityUrl"));
                channelInfo.setLoadingBgSwitch(rs.getInt("loadingBgSwitch"));
                channelInfo.setClientSwitch(rs.getString("clientSwitch"));
                map.put(channelInfo.getChannelId(), channelInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


    public Map<String, List<LightMsg>> getLightMsg(int channelId, long serverId) throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, List<LightMsg>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall("select * from t_s_light_msg where channelId = ? and startTime <= ?  and endTime >= ? order by msgId asc");
            call.setInt(1, channelId);
            String nowDate = DateHelper.getCurrentDateTimeString();
            call.setString(2, nowDate);
            call.setString(3, nowDate);

            rs = call.executeQuery();
            while (rs.next()) {
                LightMsg lightMsg = new LightMsg();
                lightMsg.setMsgId(rs.getLong("msgId"));
                lightMsg.setChannelId(rs.getInt("channelId"));
                lightMsg.setLightType(rs.getInt("lightType"));
                lightMsg.setMsgType(rs.getInt("msgType"));
                lightMsg.setServerList(rs.getString("serverList"));
                lightMsg.setLanguage(rs.getString("language"));
                lightMsg.setStartTime(rs.getDate("startTime").getTime());
                lightMsg.setEndTime(rs.getDate("endTime").getTime());
                lightMsg.setSpace(rs.getInt("space"));
                lightMsg.setContent(rs.getString("content"));

                if (lightMsg.getLightType() == eLightType.Channel.getValue() || lightMsg.getServerList().indexOf(serverId + "") >= 0) {
                    if (!map.containsKey(lightMsg.getLanguage())) {
                        map.put(lightMsg.getLanguage(), new ArrayList<>());
                    }
                    map.get(lightMsg.getLanguage()).add(lightMsg);
                }
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public List<Integer> getHideGoodsId(int channelId) throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        List<Integer> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall("select goodsId from t_s_hide_goods where channelId = ?");
            call.setInt(1, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                int goodId = rs.getInt("goodsId");
                list.add(goodId);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public Map<String, AppIdChaneelInfo> getAppIdChannelInfoMap() throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, AppIdChaneelInfo> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall("select * from t_s_appId_channel");
            rs = call.executeQuery();
            while (rs.next()) {
                int channelId = rs.getInt("channelId");
                String appId = rs.getString("appId");
                AppIdChaneelInfo appIdChaneelInfo = new AppIdChaneelInfo();
                appIdChaneelInfo.setChannelId(channelId);
                appIdChaneelInfo.setAppId(appId);
                appIdChaneelInfo.setAppIdName(rs.getString("appIdName"));
                appIdChaneelInfo.setLogoOpen(rs.getInt("logoOpen"));
                appIdChaneelInfo.setForwardServer(rs.getInt("forwardServer"));
                appIdChaneelInfo.setUserOpen(rs.getInt("userOpen"));
                appIdChaneelInfo.setSecretAgreementOpen(rs.getInt("secretAgreementOpen"));
                appIdChaneelInfo.setSecrteUrl(rs.getString("secretUrl"));
                appIdChaneelInfo.setAgreementUrl(rs.getString("agreementUrl"));
                appIdChaneelInfo.setCompanyInfo(rs.getString("companyInfo"));

                appIdChaneelInfo.setUpdateDeleteUrl(rs.getString("updateDeleteUrl"));
                appIdChaneelInfo.setLogOutUrl(rs.getString("logOutUrl"));

                appIdChaneelInfo.setHideMarks(StringUtils.stringToStringList(rs.getString("hideMarks"), ","));
                appIdChaneelInfo.setMjbUserOpen(rs.getInt("mjbUserOpen"));
                appIdChaneelInfo.setCustomerButtonShow(rs.getInt("customerButtonShow"));
                appIdChaneelInfo.setClientLoginVersion(rs.getString("clientLoginVersion"));
                appIdChaneelInfo.setCreateRoleLimit(rs.getBoolean("createRoleLimit"));
                appIdChaneelInfo.setDestroyRoleLimit(rs.getBoolean("destroyRoleLimit"));
                map.put(appIdChaneelInfo.getAppId(), appIdChaneelInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public Map<String, Map<String, ChannelNotice>> getAppIdChannelNoticeMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_appid_notice";
        Map<String,Map<String, ChannelNotice>> dataMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                String appId = rs.getString("appId");

                ChannelNotice channelNotice = new ChannelNotice();
                channelNotice.setContent(rs.getString("content"));
                channelNotice.setTitle(rs.getString("title"));
                channelNotice.setLanguage(rs.getString("languageType"));

                if (!dataMap.containsKey(appId)) {
                    dataMap.put(appId, new HashMap<>());
                }

                dataMap.get(appId).put(channelNotice.getLanguage(), channelNotice);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    /**
     * 获取金玉满堂用户列表
     * @return
     */
    public Map<String, Short> getHideJinYuUserMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_hide_jinyu";
        Map<String, Short> map = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                String platformUserId = rs.getString("platformUserId");
                map.put(platformUserId, (short) 0);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public Map<String, Map<String, String>> getFixedLanguageMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_language_fixed";
        Map<String, Map<String, String>> languageMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                String key = rs.getString("key");
                String language = rs.getString("language");
                String content = rs.getString("content");

                if (!languageMap.containsKey(key)) {
                    languageMap.put(key, new HashMap<>());
                }

                languageMap.get(key).put(language, content);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return languageMap;
    }

    public List<HideMallData> getHideMallList(int channelId) throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        List<HideMallData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall("select * from t_s_hide_mall where channelId = ?");
            call.setInt(1, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                int platformId = rs.getInt("platformId");
                int mallId = rs.getInt("mallId");
                HideMallData data = new HideMallData();
                data.setPlatform(platformId);
                data.setMallId(mallId);
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }



    public Map<Long, String> getServerNameMap(int channelId) {
        Map<Long, String> resultMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select serverId, serverName from t_s_server_list where channelId=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                long serverId = rs.getLong("serverId");
                String serverName = rs.getString("serverName");
                resultMap.put(serverId, serverName);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return resultMap;
    }

    public Map<Integer, ShieldMallMarkIdData> getShieldMallMarkIdDataList(int channelId) {
        Map<Integer, ShieldMallMarkIdData> resultMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from `t_s_shield_mall_markid` where `channelId`=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShieldMallMarkIdData data = new ShieldMallMarkIdData();
                data.setChannelId(rs.getInt("channelId"));
                data.setMallMarkId(rs.getInt("mallMarkId"));
                data.setMallName(rs.getString("mallName"));
                data.setState(rs.getInt("state"));
                data.setRemark(rs.getString("remark"));
                data.setGoodsType(rs.getInt("goodsType"));
                resultMap.put(data.getMallMarkId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }

    private static String getPlatformActivityConfigSql = "select * from t_s_platform_activity_config";
    public List<PlatformActivityConfig> getPlatformActivityConfigList() {
        List<PlatformActivityConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getPlatformActivityConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                PlatformActivityConfig platformActivityConfig = new PlatformActivityConfig();
                platformActivityConfig.setActivityBigType(rs.getInt("activityBigType"));
                platformActivityConfig.setActivityChildType(rs.getInt("activityChildType"));
                platformActivityConfig.setDesc(rs.getString("desc"));
                platformActivityConfig.setAppId(rs.getString("appId"));
                platformActivityConfig.setClientType(rs.getInt("clientType"));
                platformActivityConfig.setPhonePlatform(rs.getInt("phonePlatform"));
                list.add(platformActivityConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getActivityPosterInfoSql = "select * from `t_s_activity_post` where `activityId`=? ORDER BY `id` ASC";
    public List<ActivityPosterInfo> getActivityPosterInfoList(int activityId) {
        List<ActivityPosterInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getActivityPosterInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ActivityPosterInfo data = new ActivityPosterInfo();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setShowId(rs.getInt("showId"));
                data.setPic(rs.getInt("pic"));
                data.setPos(rs.getString("pos"));
                data.setJump(rs.getString("jump"));
                data.setBigType(rs.getInt("bigType"));
                data.setChildType(rs.getInt("childType"));
                data.setFirstShow(rs.getInt("firstShow"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public List<IntroductionShieldConfig> getIntroductionShieldConfigList() {
        String sql = "select * from t_s_introduction_shield";
        List<IntroductionShieldConfig> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                IntroductionShieldConfig data = new IntroductionShieldConfig();
                data.setActivityType(rs.getInt("activityType"));
                data.setChildType(rs.getInt("childType"));
                data.setAppId(rs.getString("appId"));
                data.setClientType(rs.getInt("clientType"));
                dataList.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public boolean updateServer(ServerInfo serverInfo) {
        PreparedStatement call = null;
        String sql = "update t_s_server_list set preRoleNum=? where serverId=? ";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++,serverInfo.getPreRoleNum());
            call.setLong(i++, serverInfo.getServerId());
            if(call.executeUpdate() > 0){
                serverInfo.setNoneOption();
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    public List<Integer> getHideGoodsIdReview(int channelId) throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        List<Integer> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall("select goodsId from t_s_hide_goods_review where channelId = ?");
            call.setInt(1, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                int goodId = rs.getInt("goodsId");
                list.add(goodId);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
